// pages/details/details.js

var WxParse = require('../../utils/wxParse/wxParse.js');
var util = require('../../utils/util.js');
var api = require('../../config/api.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {

    serviceList: '', //服务详情
    productList: '', //规格
    couponList: [], //优惠券
    countData: '', //評價
    id: "", //商品I
    num: "1", //数量
    productId: "", //产品id
    detailsList: "", //页面数据
    currentIndex: 0, //轮播设置
    chooseNum: 1,
    number: "", //轮播图片数量
    value: 3, //评级
    fwshow: false, //服务
    glshow: false, //规格
    yhshow: false, //优惠券
    freightShow: false, //运费
    parameter: [{
      id: 1,
      name: '10kg糯香型'
    }, {
      id: 2,
      name: '5kg糯香型'
    }],
    Fxshow:false,//分享状态

  },


  //获取海报背景
  getPostData() {
    let that = this;
    util.request(api.CreatePoster, {
      goodsId: that.data.id
    }).then(function(res) {
      
      if (res.errno == 0) {
        wx.setStorageSync("imgcode", res.data.QRcode);
        wx.setStorageSync("imgbg", res.data.bgImg);
        that.setData({
          isCanDraw: !that.data.isCanDraw
        })

      } else {
        util.myToast(res.errmsg)
      }
    });
  },
  //点击生成海报
  onCreatPost(e) {
    this.getPostData()
  },


// 分享弹出层
FxPopup() {
  this.setData({
    Fxshow: true
  });
},

FxClose() {
  this.setData({
    Fxshow: false
  });
},





  // 服务内容弹出层控制
  fwPopup() {
    this.setData({
      fwshow: true
    });
  },

  fwClose() {
    this.setData({
      fwshow: false
    });
  },

  // 规格内容控制
  glPopup() {
    this.setData({
      glshow: true
    });
  },

  glClose() {
    this.setData({
      glshow: false
    });
  },



  // 优惠劵
  yhPopup() {
    this.setData({
      yhshow: true
    });
  },

  yhClose() {
    this.setData({
      yhshow: false
    });
  },

  // 运费
  FreightPopup() {
    this.setData({
      freightShow: true
    });
  },

  FreightClose() {
    this.setData({
      freightShow: false
    });
  },


  // 跳转
  upAll(e) {
    let valueId = e.currentTarget.dataset.id

    wx.navigateTo({
      url: '../comment/comment?valueId=' + valueId,
    })
  },

  toCard() {
    wx.switchTab({
      url: '../shop-car/shop-car',
    })
  },




  // 星级评级
  onChange(event) {
    this.setData({
      value: event.detail,
    });
  },


  // 商品详情获取
  getData: function(id) {

    var that = this;
    util.request(api.detail, {
      id: id,
    }, 'get').then(function(res) {
      if (res.errno === 0) {
        // 富文本
        let acrticle = res.data.info.detail;
        WxParse.wxParse('article', 'html', acrticle, that, 5);
        //默认选中第一个
        res.data.productList[0].deleted = true;

        that.setData({
          detailsList: res.data.info,
          number: res.data.info.gallery.length,
          serviceList: res.data.issue,
          productList: res.data.productList,
          couponList: res.data.couponList,
          countData: res.data.comment,
          point: res.data.productList[0].exchangePoint,
          productLists: res.data.productList,
          productId: res.data.productList[0].id,
          goodsInfo: res.data.productList[0],


        })
      } else {
        wx.showToast({
          title: res.errmsg,
          icon: 'none',
          duration: 1500
        })
      }


    });
  },




  // 单选
  parameterTap: function(e) { //e是获取e.currentTarget.dataset.id所以是必备的，跟前端的data-id获取的方式差不多
    var that = this
    var this_checked = e.currentTarget.dataset.id
    var index = e.currentTarget.dataset.index

    var parameterList = this.data.productLists //获取Json数组


    for (var i = 0; i < parameterList.length; i++) {
      if (parameterList[i].id == this_checked) {
        parameterList[i].deleted = true; //当前点击的位置为true即选中
      } else {
        parameterList[i].deleted = false; //其他的位置为false
      }
    }
    that.setData({
      productLists: parameterList,
      productId: e.currentTarget.dataset.id,
      goodsInfo: parameterList[index]
    })
  },


  Change(event) {
    let numbers = event.detail
    this.setData({
      num: numbers
    })
  },

  // 计步器
  Change(event) {
    let numbers = event.detail
    this.setData({
      num: numbers
    })
  },


  // 加入购物车
  getAddCartData(e) {
    let that = this;
    let goodsId = e.currentTarget.dataset.goodsid
    util.request(api.CartAdd, {
      productId: that.data.productId,
      goodsId: goodsId,
      number: that.data.num //测试数据。默认一条
    }, 'post').then(function(res) {
      if (res.errno == 0) {
        wx.showToast({
          title: '加入购物车',
        })

        // 关闭规格栏
        that.glClose()

      } else {
        util.myToast(res.errmsg)
      }
    });
  },





  // 快速加入购物车
  fastAdd(e) {
    console.log(e)
    let that = this;
    let goodsId = e.currentTarget.dataset.goodsid
    util.request(api.Fastadd, {
      productId: that.data.productId,
      goodsId: goodsId,
      number: that.data.num //测试数据。默认一条
    }, 'post').then(function(res) {
      if (res.errno == 0) {
        wx.showToast({
          title: '下单成功',
        })
        wx.navigateTo({
          url: '../submit-order/submit-order?cartId=' + res.data,
        })

        // 关闭规格栏
        that.glClose()

      } else {
        util.myToast(res.errmsg)
      }
    });
  },

  // 领取优惠券
  getReceive(e) {
    let that = this;
    let couponid = e.currentTarget.dataset.couponid
    util.request(api.Receive, {
      couponId: couponid,
    }, 'post').then(function(res) {
      if (res.errno == 0) {
        wx.showToast({
          title: '领取成功',
        })

        that.getData(that.data.id)

      } else {
        util.myToast(res.errmsg)
      }
    });
  },




  /**
   * 生命周期函数--监听页面加载
   */

  onLoad: function(options) {

    // 获取商品id
    var id = options.goodsid;
    this.setData({
      id: id,
    })
    //获取通过分享进入小程序携带的参数
    var uid = options.userid;
    const scene = decodeURIComponent(options.scene)
    console.log("scene:" + scene);
    if (uid == undefined) {
      uid = ""
    }
    wx.setStorageSync("shareUserId", uid)
    if (scene != undefined && scene != "undefined") {
      var arrPara = scene.split(",");

      var arrUserid = arrPara[0].split(":");
      var arrGoodsid = arrPara[1].split(":");
      this.setData({
        id: parseInt(arrGoodsid[1]),
      });
      //存入上级代理的UserID
      wx.setStorageSync("shareUserId", arrUserid[1])

    };
    //分享到朋友圈
    wx.showShareMenu({
      withShareTicket: true,
      menus: ['shareAppMessage', 'shareTimeline']
    })
  },
  //赠送礼品
  onSend(e) {
    var id = this.data.goodsInfo.id;
    var num = this.data.num;
    var products = [];
    var obj = {
      productId: '',
      num: ''
    }
    obj.productId = id;
    obj.num = num;
    products.push(obj);
    
    util.toClass('/pages/submit-order-send/submit-order-send',{
      products: JSON.stringify(products) 
    })
  },


  // 视频播放
  videoPlay: function() {
    console.log("开始播放")
    var videoplay = wx.createVideoContext("video");
    videoplay.play()
    this.setData({
      controls: true,
    })
  },




  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */

  onShow: function() {
    // 商品详情查询
    this.getData(this.data.id)



  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {
    return {
      title: "傣王稻勐海香米，一键领取优惠",
      path: '/pages/index/index?userid=' + wx.getStorageSync("userid"),
      imageUrl: 'https://zebra-1258707023.cos.ap-chengdu.myqcloud.com/yndwd/share-img.jpg'
    }
  }
})